module ucordic8 (CLK, nRESET, EXTDataIn, EXTDataOut, SELMUXEXTREGX, SELMUXEXTREGY, SELMUXEXTREGZ, SELMUXREGEXT, ENX, ENY, ENZ, ApproxMode, CoordMode, nCTRHOLD);

input CLK, nRESET;
input [7:0] EXTDataIn;
output [7:0] EXTDataOut;
input SELMUXEXTREGX, SELMUXEXTREGY, SELMUXEXTREGZ;
input [1:0] SELMUXREGEXT;
input ENX, ENY, ENZ;
input ApproxMode;
input [1:0] CoordMode;
input nCTRHOLD;

wire CLK, nRESET;
wire [7:0] EXTDataIn;
wire [7:0] EXTDataOut;
wire SELMUXEXTREGX, SELMUXEXTREGY, SELMUXEXTREGZ;
wire [1:0] SELMUXREGEXT;
wire ENX, ENY, ENZ;
wire ApproxMode;
wire [1:0] CoordMode;
wire nCTRHOLD;

wire [7:0] RegXToShifterXAdderX, ShifterXToAdderY, AdderYToMuxExtRegY, RegYToShifterYAdderY;
wire [7:0] ShifterYToAdderX, AdderXToMuxExtRegX;
wire [2:0] CounterToROMFiROMATAN, ROMFiToShifter;
wire [7:0] RegZToAdderZ, ROMATANToAdderZ, AdderZToMuxExtRegZ;
reg ZRY;
wire ModeX, ModeY, ModeZ;
wire [7:0] MuxExtRegYToRegY, MuxExtRegXToRegX, MuxExtRegZToRegZ;

always @ (RegYToShifterYAdderY)
begin
	ZRY = ~(| RegYToShifterYAdderY);
end

reg8 REGX (.Y(RegXToShifterXAdderX), .A(MuxExtRegXToRegX), .CLK(CLK), .nRESET(nRESET), .EN(ENX));
reg8 REGY (.A(MuxExtRegYToRegY), .Y(RegYToShifterYAdderY), .CLK(CLK), .nRESET(nRESET), .EN(ENY));
reg8 REGZ (.Y(RegZToAdderZ), .A(MuxExtRegZToRegZ), .CLK(CLK), .nRESET(nRESET), .EN(ENZ));

addsub8 ADDERX (.A(RegXToShifterXAdderX), .B(ShifterYToAdderX), .Y(AdderXToMuxExtRegX), .M(ModeX), .CBi(1'b0), .CBo(), .OV());
addsub8 ADDERY (.B(ShifterXToAdderY), .Y(AdderYToMuxExtRegY), .A(RegYToShifterYAdderY), .M(ModeY), .CBi(1'b0), .CBo(), .OV());
addsub8 ADDERZ (.A(RegZToAdderZ), .B(ROMATANToAdderZ), .Y(AdderZToMuxExtRegZ), .M(ModeZ), .CBi(1'b0), .CBo(), .OV());

srshifter SHIFTERX (.A(RegXToShifterXAdderX), .Y(ShifterXToAdderY), .SEL(ROMFiToShifter));
srshifter SHIFTERY (.A(RegYToShifterYAdderY), .Y(ShifterYToAdderX), .SEL(ROMFiToShifter));

romfi ROMFi (.ADDR(CounterToROMFiROMATAN), .DATA(ROMFiToShifter), .MODE(CoordMode));
romatantable ROMATAN (.ADDR(CounterToROMFiROMATAN), .DATA(ROMATANToAdderZ), .MODE(CoordMode));

counter3b COUNTER (.P(CounterToROMFiROMATAN), .nCLR(nRESET), .CLK(CLK), .nHOLD(nCTRHOLD));

ctlmodule MODEGENERATOR (.MSBY(RegYToShifterYAdderY[7]), .MSBZ(RegZToAdderZ[7]), .ZRY(ZRY), .ModeX(ModeX), .ModeY(ModeY), .ModeZ(ModeZ), .ApproxMode(ApproxMode), .CoordMode(CoordMode));

muxextreg MUXEXTREGX (.A(AdderXToMuxExtRegX), .B(EXTDataIn), .Y(MuxExtRegXToRegX), .SEL(SELMUXEXTREGX));
muxextreg MUXEXTREGY (.A(AdderYToMuxExtRegY), .B(EXTDataIn), .Y(MuxExtRegYToRegY), .SEL(SELMUXEXTREGY));
muxextreg MUXEXTREGZ (.A(AdderZToMuxExtRegZ), .B(EXTDataIn), .Y(MuxExtRegZToRegZ), .SEL(SELMUXEXTREGZ));
muxregext MUXREGEXT (.X(RegXToShifterXAdderX), .Y(RegYToShifterYAdderY), .Z(RegZToAdderZ), .SEL(SELMUXREGEXT), .OUT(EXTDataOut));

endmodule